package com.qd.panda.my;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.qd.common.panda.domain.entity.transfer.TransferDTO;
import com.qd.common.panda.domain.entity.transfer.UserDividedDTO;
import com.qd.common.panda.domain.entity.transfer.UserToBeTransferDTO;
import com.qd.common.panda.domain.entity.transfer.UserTransferDTO;
import com.qd.common.sys.annotation.OperatorInfo;
import com.qd.common.sys.enums.OperatorType;
import com.qd.panda.service.transfer.UserTransferService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 短期租赁/WEB_转账记录
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/pandaUserTransfer/")
@Slf4j
public class UserTransferController {
    private final UserTransferService userTransferService;

    /**
     * 待转账记录列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/toBeTransferPage")
    public IPage<UserToBeTransferDTO> userToBeTransferPage(UserToBeTransferDTO dto) {
        return userTransferService.userToBeTransferPage(PageUtil.buildPage(dto), dto);
    }

    /**
     * 待转账记录导出
     *
     * @param query 查询参数
     */
    @PreAuthorize("@ps.hasPermission('system:userTransfer:toBeTransferExport')")
    @OperatorInfo(title = "待转账记录导出", operator = OperatorType.EXPORT)
    @GetMapping(value = "/toBeTransferExport")
    public void toBeTransferExport(UserToBeTransferDTO query) {
        userTransferService.toBeTransferExport(query);
    }

    /**
     * 待转账记录详情
     *
     * @param userId 用户ID
     * @return 列表信息
     */
    @GetMapping(value = "/toBeTransferByUserId")
    public UserToBeTransferDTO toBeTransferByUserId(@Validated @NotBlank(message = "用户ID不能为空") String userId) {
        return userTransferService.userToBeTransferByUserId(userId);
    }

    /**
     * 转账
     *
     * @param transferDTO 参数
     * @return true or false
     */
    @PreAuthorize("@ps.hasPermission('system:userTransfer:saveTransfer')")
    @OperatorInfo(title = "转账", operator = OperatorType.INSERT)
    @PostMapping(value = "/saveTransfer")
    public ApiResultWrapper<Object> saveTransfer(@Validated({DefaultGroup.class}) @RequestBody TransferDTO transferDTO) {
        userTransferService.saveTransfer(transferDTO.getList());
        return ApiResult.success();
    }

    /**
     * 已转账记录列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "dividedPage")
    public IPage<UserDividedDTO> dividedPage(UserDividedDTO dto) {
        return userTransferService.dividedPage(PageUtil.buildPage(dto), dto);
    }

    /**
     * 已转账明细
     *
     * @param userId 用户ID
     * @return 列表信息
     */
    @GetMapping(value = "/dividedDetailList")
    public List<UserTransferDTO> dividedDetailList(@Validated @NotBlank(message = "用户ID不能为空") String userId) {
        UserTransferDTO query = new UserTransferDTO();
        query.setUserId(userId);
        List<UserTransferDTO> list = userTransferService.getListDto(query);
        query.freeData();
        return list;
    }

    /**
     * 已转账记录导出
     *
     * @param query 查询参数
     */
    @PreAuthorize("@ps.hasPermission('system:userTransfer:dividedExport')")
    @OperatorInfo(title = "已转账记录导出", operator = OperatorType.EXPORT)
    @GetMapping(value = "/dividedExport")
    public void dividedExport(UserDividedDTO query) {
        userTransferService.dividedExport(query);
    }
}